package com.atguigu.gulimall.product.app;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.atguigu.gulimall.product.entity.AttrEntity;
import com.atguigu.gulimall.product.service.AttrAttrgroupRelationService;
import com.atguigu.gulimall.product.service.AttrService;
import com.atguigu.gulimall.product.service.CategoryService;
import com.atguigu.gulimall.product.viewVo.AttrGroupRelationVo;
import com.atguigu.gulimall.product.viewVo.AttrGroupWithAttrsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.atguigu.gulimall.product.entity.AttrGroupEntity;
import com.atguigu.gulimall.product.service.AttrGroupService;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Message;



/**
 * 属性分组
 *
 * @author xiaolei
 * @email qingyulei@yeah.net
 * @date 2020-08-22 23:46:20
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private AttrService attrService;

    @Autowired
    private AttrAttrgroupRelationService attrAttrgroupRelationService;


    ///product/attrgroup/{attrgroupId}/attr/relation
    @GetMapping("/{attrgroupId}/attr/relation")
    public Message attrRelation(@PathVariable("attrgroupId") Long attrgroupId) {

        List<AttrEntity> attrEntities = attrService.getRelationAttr(attrgroupId);
        return Message.ok().put("data", attrEntities);
    }
    /**
     * 列表
     */
    @RequestMapping("/list/{catelogId}")
    //@RequiresPermissions("product:attrgroup:list")
    public Message list(@PathVariable("catelogId") int catelogId,
                        @RequestParam Map<String, Object> params){
        //PageUtils page = attrGroupService.queryPage(params);
        PageUtils page = attrGroupService.queryPage(params, catelogId);
        return Message.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    //@RequiresPermissions("product:attrgroup:info")
    public Message info(@PathVariable("attrGroupId") Long attrGroupId){

		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        attrGroup.setCatelogPath(categoryService.findCatelogPath(attrGroup.getCatelogId()));
        return Message.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:attrgroup:save")
    public Message save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return Message.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:attrgroup:update")
    public Message update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return Message.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:attrgroup:delete")
    public Message delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return Message.ok();
    }

    ///product/attrgroup/attr/relation/delete
    @PostMapping("/attr/relation/delete")
    public Message deleteRelation(@RequestBody AttrGroupRelationVo[] vos) {

        attrService.deleteRelation(vos);
        return Message.ok();
    }

    ///product/attrgroup/{attrgroupId}/noattr/relation
    @GetMapping("/{attrgroupId}/noattr/relation")
    public Message attrNoRelation(@PathVariable("attrgroupId") Long attrgroupId,
                                  @RequestParam Map<String, Object> params) {

        PageUtils pageUtils = attrService.getNoRelationAttr(params, attrgroupId);
        return Message.ok().put("page", pageUtils);
    }

    ///product/attrgroup/attr/relation
    @PostMapping("/attr/relation")
    public Message attrNoRelation(@RequestBody List<AttrGroupRelationVo> voList) {

        attrAttrgroupRelationService.saveBatch(voList);
        return Message.ok();
    }

    ///product/attrgroup/{catelogId}/withattr
    @GetMapping("/{catelogId}/withattr")
    public Message getAttrGroupWithAttrsByCatelogId(@PathVariable("catelogId") Long catelogId) {

        List<AttrGroupWithAttrsVo> attrsList = attrGroupService.getAttrGroupWithAttrsByCatelogId(catelogId);
        return Message.ok().put("data", attrsList);
    }
}
